package com.xcc.seata.order.service.impl;

import com.xcc.seata.order.dao.OrderDao;
import com.xcc.seata.order.domain.Order;
import com.xcc.seata.order.service.AccountService;
import com.xcc.seata.order.service.OrderService;
import com.xcc.seata.order.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;
    @Autowired
    AccountService accountService;
    @Autowired
    StorageService storageService;

    @Override
    @GlobalTransactional(name = "my-order-test",rollbackFor = Exception.class)
    public Long create(Order order) {
        log.info("=========================下订单，开始");
        orderDao.create(order);
        log.info("=========================下订单，完成");

        log.info("=========================减库存，开始");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("=========================减库存，完成");

        log.info("=========================减积分，开始");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("=========================减积分，完成");

        log.info("=========================订单状态修改，开始");
        orderDao.update(order.getId(),1);
        log.info("=========================订单状态修改，完成");

        return order.getId();
    }

    @Override
    public void update(Long id, Integer status) {
        orderDao.update(id,status);
    }
}
